﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class OrderSelection : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbIsActive;
        protected CheckBox chbSysCreate;
        protected HtmlForm F7021;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnOrderID;
        protected Label lblCarrierIdEnd;
        protected Label lblCarrierIdStart;
        protected Label lblCityBegin;
        protected Label lblCityEnd;
        protected Label lblCompanyEnd;
        protected Label lblCompanyStart;
        protected Label lblDeliveryDataEnd;
        protected Label lblDeliveryDataStart;
        protected Label lblDescr;
        protected Label lblDoorEnd;
        protected Label lblDoorStart;
        protected Label lblExternOrderIdEnd;
        protected Label lblExternOrderIdStart;
        protected Label lblFrom;
        protected Label lblLoadIdEnd;
        protected Label lblLoadIdStart;
        protected Label lblLotEnd;
        protected Label lblLotStart;
        protected Label lblMsg;
        protected Label lblOrderDateEnd;
        protected Label lblOrderDateStart;
        protected Label lblOrderIdEnd;
        protected Label lblOrderIdStart;
        protected Label lblOrderPriorityEnd;
        protected Label lblOrderPriorityStart;
        protected Label lblOrderSelectionID;
        protected Label lblOrderStatusEnd;
        protected Label lblOrderStatusStart;
        protected Label lblOrderTypeEnd;
        protected Label lblOrderTypeStart;
        protected Label lblOwner;
        protected Label lblOwnerIdEnd;
        protected Label lblOwnerIdStart;
        protected Label lblRouteEnd;
        protected Label lblRouteStart;
        protected Label lblSkuClassIdEnd;
        protected Label lblSkuClassIdStart;
        protected Label lblSkuIdEnd;
        protected Label lblSkuIdStart;
        protected Label lblStopEnd;
        protected Label lblStopStart;
        protected Label lblTo;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCarrierIdEnd;
        protected TextBox txtCarrierIdStart;
        protected TextBox txtCityEnd;
        protected TextBox txtCityStart;
        protected TextBox txtCompanyEnd;
        protected TextBox txtCompanyStart;
        protected TextBox txtDeliveryDataEnd;
        protected TextBox txtDeliveryDataStart;
        protected TextBox txtDescr;
        protected TextBox txtDoorEnd;
        protected TextBox txtDoorStart;
        protected TextBox txtExternOrderIdEnd;
        protected TextBox txtExternOrderIdStart;
        protected TextBox txtLoadIdEnd;
        protected TextBox txtLoadIdStart;
        protected TextBox txtLotEnd;
        protected TextBox txtLotStart;
        protected TextBox txtOrderDateEnd;
        protected TextBox txtOrderDateStart;
        protected TextBox txtOrderIdEnd;
        protected TextBox txtOrderIdStart;
        protected TextBox txtOrderPriorityEnd;
        protected TextBox txtOrderPriorityStart;
        protected TextBox txtOrderSelectionID;
        protected TextBox txtOrderStatusEnd;
        protected TextBox txtOrderStatusStart;
        protected TextBox txtOrderTypeEnd;
        protected TextBox txtOrderTypeStart;
        protected TextBox txtOwnerIdEnd;
        protected TextBox txtOwnerIdStart;
        protected TextBox txtRouteEnd;
        protected TextBox txtRouteStart;
        protected TextBox txtSkuClassIdEnd;
        protected TextBox txtSkuClassIdStart;
        protected TextBox txtSkuIdEnd;
        protected TextBox txtSkuIdStart;
        protected TextBox txtStopEnd;
        protected TextBox txtStopStart;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtOrderSelectionID.Text = "";
            this.txtOrderSelectionID.Enabled = false;
            this.chbSysCreate.Checked = false;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.chbSysCreate.Checked)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7200_006");
                }
                else if (!string.IsNullOrEmpty(this.txtOrderSelectionID.Text.Trim()))
                {
                    List<string> orderSelectionID = new List<string> {
                        this.txtOrderSelectionID.Text.Trim()
                    };
                    this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().DeleteOrderSelectionInfos(orderSelectionID) + base.GetGlobalMsgResource("F7200_007");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7200_008") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("OrderSelectionQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("OrderSelectionList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (base.DataEntityViewState != null)
            {
                this.UpdateOrderSelection();
            }
            else
            {
                this.InsertOrderSelection();
            }
        }

        private void InsertOrderSelection()
        {
            try
            {
                OrderSelectionInfo orderSelection = new OrderSelectionInfo();
                orderSelection = this.SetOrderSelectionFromPage(orderSelection);
                orderSelection.IsDefault = "Y";
                orderSelection.CreatedBy = base.UserInfo.UserID;
                string orderSelectionID = OutboundServiceClientProxy.GetInstance().InsertOrderSelectionInfo(orderSelection);
                this.txtOrderSelectionID.Text = orderSelectionID;
                this.lblMsg.Text = orderSelectionID + base.GetGlobalMsgResource("F7200_002");
                base.DataEntityViewState = orderSelection;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7200_003") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetOrderSelectionInfo2Form();
                this.txtOrderSelectionID.Enabled = false;
            }
            else
            {
                string strDate = base.GetValueFromAppSettings("ORDER_SELECTION_ADVANCE_STARTDATE");
                if (strDate != "0")
                {
                    this.txtOrderDateStart.Text = base.GetPreferenceShortDate(DateTime.Now.AddDays(double.Parse(strDate) * -1.0));
                    this.txtDeliveryDataStart.Text = base.GetPreferenceShortDate(DateTime.Now.AddDays(double.Parse(strDate) * -1.0));
                    this.txtOrderDateEnd.Text = base.GetPreferenceShortDate(DateTime.Now);
                    this.txtDeliveryDataEnd.Text = base.GetPreferenceShortDate(DateTime.Now);
                }
                else
                {
                    this.txtOrderDateStart.Text = "";
                    this.txtDeliveryDataStart.Text = "";
                    this.txtOrderDateEnd.Text = "";
                    this.txtDeliveryDataEnd.Text = "";
                }
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private OrderSelectionInfo SetOrderSelectionFromPage(OrderSelectionInfo orderSelectionInfo)
        {
            DateTime dt = DateTime.Now;
            OrderSelectionInfo orderSelection = orderSelectionInfo;
            orderSelection.OrderSelectionID = this.txtOrderSelectionID.Text;
            orderSelection.Descr = this.txtDescr.Text;
            orderSelection.IsActive = this.chbIsActive.Checked ? "Y" : "N";
            orderSelection.SystemCreated = this.chbSysCreate.Checked ? "Y" : "N";
            orderSelection.OrderIdStart = this.txtOrderIdStart.Text;
            orderSelection.OrderIdEnd = this.txtOrderIdEnd.Text;
            orderSelection.OwnerIdStart = this.txtOwnerIdStart.Text;
            orderSelection.OwnerIdEnd = this.txtOwnerIdEnd.Text;
            orderSelection.OrderStatusStart = this.txtOrderStatusStart.Text;
            orderSelection.OrderStatusEnd = this.txtOrderStatusEnd.Text;
            orderSelection.OrderTypeStart = this.txtOrderTypeStart.Text;
            orderSelection.OrderTypeEnd = this.txtOrderTypeEnd.Text;
            if (!string.IsNullOrEmpty(this.txtOrderDateStart.Text))
            {
                orderSelection.OrderDateStart = DateTime.Parse(this.txtOrderDateStart.Text);
            }
            else
            {
                orderSelection.OrderDateStart = DateTime.Parse("1900/01/01 00:00:00");
            }
            if (!string.IsNullOrEmpty(this.txtOrderDateEnd.Text))
            {
                orderSelection.OrderDateEnd = DateTime.Parse(this.txtOrderDateEnd.Text);
            }
            else
            {
                orderSelection.OrderDateEnd = DateTime.Parse("2100/01/01 00:00:00");
            }
            if (!string.IsNullOrEmpty(this.txtDeliveryDataStart.Text))
            {
                orderSelection.DeliveryDateStart = DateTime.Parse(this.txtDeliveryDataStart.Text);
            }
            else
            {
                orderSelection.DeliveryDateStart = DateTime.Parse("1900/01/01 00:00:00");
            }
            if (!string.IsNullOrEmpty(this.txtDeliveryDataEnd.Text))
            {
                orderSelection.DeliveryDateEnd = DateTime.Parse(this.txtDeliveryDataEnd.Text);
            }
            else
            {
                orderSelection.DeliveryDateEnd = DateTime.Parse("2100/01/01 00:00:00");
            }
            orderSelection.ExternOrderIdStart = this.txtExternOrderIdStart.Text;
            orderSelection.ExternOrderIdEnd = this.txtExternOrderIdEnd.Text;
            orderSelection.OrderPriorityStart = this.txtOrderPriorityStart.Text;
            orderSelection.OrderPriorityEnd = this.txtOrderPriorityEnd.Text;
            orderSelection.CompanyStart = this.txtCompanyStart.Text;
            orderSelection.CompanyEnd = this.txtCompanyEnd.Text;
            orderSelection.CityBegin = this.txtCityStart.Text;
            orderSelection.CityEnd = this.txtCityEnd.Text;
            orderSelection.CarrierIdStart = this.txtCarrierIdStart.Text;
            orderSelection.CarrierIdEnd = this.txtCarrierIdEnd.Text;
            orderSelection.DoorStart = this.txtDoorStart.Text;
            orderSelection.DoorEnd = this.txtDoorStart.Text;
            orderSelection.RouteStart = this.txtRouteStart.Text;
            orderSelection.RouteEnd = this.txtRouteEnd.Text;
            orderSelection.StopStart = this.txtStopStart.Text;
            orderSelection.StopEnd = this.txtStopEnd.Text;
            orderSelection.LoadIdStart = this.txtLoadIdStart.Text;
            orderSelection.LoadIdEnd = this.txtLoadIdEnd.Text;
            orderSelection.SkuIdStart = this.txtSkuIdStart.Text;
            orderSelection.SkuIdEnd = this.txtSkuIdEnd.Text;
            orderSelection.LotStart = this.txtLotStart.Text;
            orderSelection.LotEnd = this.txtLotEnd.Text;
            orderSelection.SkuClassIdStart = this.txtSkuClassIdStart.Text;
            orderSelection.SkuClassIdEnd = this.txtSkuClassIdEnd.Text;
            orderSelection.Descr = this.txtDescr.Text;
            orderSelection.OrderGroupStart = "";
            orderSelection.OrderGroupEnd = "";
            orderSelection.ConsigneeIdStart = "";
            orderSelection.ConsigneeIdEnd = "";
            orderSelection.StateStart = "";
            orderSelection.StateEnd = "";
            orderSelection.ZipStart = "";
            orderSelection.ZipEnd = "";
            orderSelection.CartonGroup = "";
            orderSelection.MaxOrders = 0;
            orderSelection.BatchPickMaxCount = 0.0M;
            orderSelection.BatchPickMaxCube = 0.0M;
            orderSelection.EnablePacking = "";
            orderSelection.OrderHandingTypeStart = "";
            orderSelection.OrderHandingTypeEnd = "";
            orderSelection.SkuGroupStart = "";
            orderSelection.SkuGroupEnd = "";
            orderSelection.SkuGroup2Start = "";
            orderSelection.SkuGroup2End = "";
            orderSelection.AbcStart = "";
            orderSelection.AbcEnd = "";
            orderSelection.Udf1Start = "";
            orderSelection.Udf1End = "";
            orderSelection.Udf2Start = "";
            orderSelection.Udf2End = "";
            orderSelection.Udf3Start = "";
            orderSelection.Udf3End = "";
            orderSelection.Udf4Start = "";
            orderSelection.Udf4End = "";
            orderSelection.Udf5Start = "";
            orderSelection.Udf5End = "";
            return orderSelection;
        }

        private void SetOrderSelectionInfo2Form()
        {
            try
            {
                string orderSelectionID = base.Request.QueryString["ID"].ToString();
                OrderSelectionInfo orderSelection = OutboundServiceClientProxy.GetInstance().GetOrderSelectionByID(orderSelectionID);
                base.DataEntityViewState = orderSelection;
                if (orderSelection != null)
                {
                    this.txtOrderSelectionID.Text = orderSelection.OrderSelectionID;
                    this.txtDescr.Text = orderSelection.Descr;
                    this.chbIsActive.Checked = orderSelection.IsActive == "Y";
                    this.chbSysCreate.Checked = orderSelection.SystemCreated == "Y";
                    this.txtOrderIdStart.Text = orderSelection.OrderIdStart;
                    this.txtOrderIdEnd.Text = orderSelection.OrderIdEnd;
                    this.txtOwnerIdStart.Text = orderSelection.OwnerIdStart;
                    this.txtOwnerIdEnd.Text = orderSelection.OwnerIdEnd;
                    this.txtOrderStatusStart.Text = orderSelection.OrderStatusStart;
                    this.txtOrderStatusEnd.Text = orderSelection.OrderStatusEnd;
                    this.txtOrderTypeStart.Text = orderSelection.OrderTypeStart;
                    this.txtOrderTypeEnd.Text = orderSelection.OrderTypeEnd;
                    this.txtOrderDateStart.Text = base.GetPreferenceShortDate(orderSelection.OrderDateStart);
                    this.txtOrderDateEnd.Text = base.GetPreferenceShortDate(orderSelection.OrderDateEnd);
                    this.txtDeliveryDataStart.Text = base.GetPreferenceShortDate(orderSelection.DeliveryDateStart);
                    this.txtDeliveryDataEnd.Text = base.GetPreferenceShortDate(orderSelection.DeliveryDateEnd);
                    this.txtExternOrderIdStart.Text = orderSelection.ExternOrderIdStart;
                    this.txtExternOrderIdEnd.Text = orderSelection.ExternOrderIdEnd;
                    this.txtOrderPriorityStart.Text = orderSelection.OrderPriorityStart;
                    this.txtOrderPriorityEnd.Text = orderSelection.OrderPriorityEnd;
                    this.txtCompanyStart.Text = orderSelection.CompanyStart;
                    this.txtCompanyEnd.Text = orderSelection.CompanyEnd;
                    this.txtCityStart.Text = orderSelection.CityBegin;
                    this.txtCityEnd.Text = orderSelection.CityEnd;
                    this.txtCarrierIdStart.Text = orderSelection.CarrierIdStart;
                    this.txtCarrierIdEnd.Text = orderSelection.CarrierIdEnd;
                    this.txtDoorStart.Text = orderSelection.DoorStart;
                    this.txtDoorStart.Text = orderSelection.DoorEnd;
                    this.txtRouteStart.Text = orderSelection.RouteStart;
                    this.txtRouteEnd.Text = orderSelection.RouteEnd;
                    this.txtStopStart.Text = orderSelection.StopStart;
                    this.txtStopEnd.Text = orderSelection.StopEnd;
                    this.txtLoadIdStart.Text = orderSelection.LoadIdStart;
                    this.txtLoadIdEnd.Text = orderSelection.LoadIdEnd;
                    this.txtSkuIdStart.Text = orderSelection.SkuIdStart;
                    this.txtSkuIdEnd.Text = orderSelection.SkuIdEnd;
                    this.txtLotStart.Text = orderSelection.LotStart;
                    this.txtLotEnd.Text = orderSelection.LotEnd;
                    this.txtSkuClassIdStart.Text = orderSelection.SkuClassIdStart;
                    this.txtSkuClassIdEnd.Text = orderSelection.SkuClassIdEnd;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateOrderSelection()
        {
            try
            {
                OrderSelectionInfo orderSelection = base.DataEntityViewState as OrderSelectionInfo;
                orderSelection = this.SetOrderSelectionFromPage(orderSelection);
                orderSelection.UpdatedBy = orderSelection.CreatedBy;
                orderSelection.IsDefault = "Y";
                this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().UpdateOrderSelectionInfo(orderSelection) + base.GetGlobalMsgResource("F7200_004");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7200_005") + ex.Message;
            }
        }
    }
}

